/*
 * $Id$
 */
package py.windmill.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import py.windmill.resolution.HTML;
import py.windmill.resolution.JSON;
import py.windmill.validation.Validator;

/**
 *
 * @author myang
 */
public class ActionSupport implements Action {

    private static List<Resolution> DEFAULT_RESOLUTIONS
            = Arrays.<Resolution>asList(new JSON(), new HTML());

    @Override
    public Object onGet(final Request req) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

    @Override
    public Object onPost(final Request req) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

    @Override
    public ValidationErrors validate(final Request req) {
        return null;
    }

    /**
     * 
     * @param req
     * @param name
     * @param validators
     * @return 
     */
    protected ValidationErrors validate(final Request req, 
                                        final String name, 
                                        final Validator... validators) {
        final ValidationErrors errors = new ValidationErrors();
        final String val = req.param(name);
        for (final Validator v : validators) {
            if (!v.validate(val)) {
                errors.put(name, v.message());
            }
        }
        return errors;
    }

    @Override
    public String boundTo() {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

    @Override
    public List<Resolution> produces() {
        return DEFAULT_RESOLUTIONS;
    }

    /**
     * 
     * @param resolution
     * @return 
     */
    protected List<Resolution> produces(final Resolution... resolution) {
        final List<Resolution> resolutions = new ArrayList();
        resolutions.addAll(Arrays.asList(resolution));
        resolutions.addAll(DEFAULT_RESOLUTIONS);
        return resolutions;
    }
}
